package com.example.ai.entity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 实现有界队列（保存最近N条对话）
 */
public class BoundedDialogueQueue {
    private final int capacity;
    private final Queue<DialogueMessage> queue;

    public BoundedDialogueQueue(int capacity) {
        this.capacity = capacity;
        this.queue = new LinkedList<>();
    }

    public synchronized void add(DialogueMessage message) {
        if (queue.size() >= capacity) {
            queue.poll(); // 移除最旧的消息
        }
        queue.offer(message);
    }

    public synchronized List<DialogueMessage> getMessages() {
        return new ArrayList<>(queue);
    }

    public synchronized void clear() {
        queue.clear();
    }
}
